package jmine.tec.database.query;

import java.util.HashMap;
import java.util.Map;

/**
 * Representa uma entrada no banco, uma linha.
 * 
 * @author lundberg
 */
public class Entry {

    private final Table table;

    private final Map<String, String> fields = new HashMap<String, String>();

    /**
     * Construtor
     * 
     * @param table table
     */
    public Entry(Table table) {
        this.table = table;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return String.format("Entry:%s,%s", this.table, this.fields);
    }

    /**
     * Adiciona o valor de um campo
     * 
     * @param key key
     * @param value value
     */
    public void addField(String key, String value) {
        this.fields.put(key, value);
    }

    /**
     * Obtém o valor da coluna
     * 
     * @param columnName columnName
     * @return String
     */
    public String getValue(String columnName) {
        return this.fields.get(columnName);
    }

    /**
     * Obtém o valor da coluna
     * 
     * @param column column
     * @return String
     */
    public String getValue(Column column) {
        return this.getValue(column.getName());
    }

    /**
     * @return the table
     */
    public Table getTable() {
        return this.table;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Entry)) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        Entry o = (Entry) obj;
        return this.table.equals(o.table) && this.fields.equals(o.fields);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.fields == null) ? 0 : this.fields.hashCode());
        result = prime * result + ((this.table == null) ? 0 : this.table.hashCode());
        return result;
    }
}
